In [2]:
import numpy as np

a = np.array([1,2,3,4])
print(a)


[1 2 3 4]

In [10]:
import time

a = np.random.rand(1000000)
b = np.random.rand(1000000)

tic = time.time()
c = np.dot(a,b)
toc = time.time()

print(c)
print("vectorized version:" + str(1000*(toc-tic)) + "ms")

c = 0
tic = time.time()
for i in range(1000000):
    c += a[i]*b[i]
toc = time.time()

print(c)
print("for loop version:" + str(1000*(toc-tic)) + "ms")


250302.977152
vectorized version:1.2450218200683594ms
250302.977152
for loop version:511.57093048095703ms

14 Broadcasting example


In [12]:
import numpy as np

A = np.array([[56.0, 0.0, 4.4, 68.0],
              [1.2,104.0,52.0,8.0],
              [1.8,135.0,99.0,0.9]])
print(A)


[[  56.     0.     4.4   68. ]
 [   1.2  104.    52.     8. ]
 [   1.8  135.    99.     0.9]]

In [13]:
cal = A.sum(axis=0)
print(cal)


[  59.   239.   155.4   76.9]

In [14]:
percentage = 100*A/cal.reshape(1,4)
print(percentage)


[[ 94.91525424   0.           2.83140283  88.42652796]
 [  2.03389831  43.51464435  33.46203346  10.40312094]
 [  3.05084746  56.48535565  63.70656371   1.17035111]]

15 Note on numpy


In [1]:
import numpy as np

a = np.random.randn(5)

In [2]:
print(a)


[-2.07466393 -1.42358358 -0.06499076 -1.83822409 -0.17734999]

In [3]:
print(a.shape)


(5,)

In [4]:
print(a.T)


[-2.07466393 -1.42358358 -0.06499076 -1.83822409 -0.17734999]

In [5]:
print(np.dot(a,a.T))


9.7455652578

In [6]:
a = np.random.randn(5,1)
print(a)


[[-0.01554998]
 [-1.8951361 ]
 [ 0.74293626]
 [-0.71214972]
 [ 1.03228842]]

In [7]:
print(a.T)


[[-0.01554998 -1.8951361   0.74293626 -0.71214972  1.03228842]]

In [8]:
print(np.dot(a,a.T))


[[  2.41801799e-04   2.94693237e-02  -1.15526421e-02   1.10739121e-02
   -1.60520616e-02]
 [  2.94693237e-02   3.59154085e+00  -1.40796533e+00   1.34962064e+00
   -1.95632705e+00]
 [ -1.15526421e-02  -1.40796533e+00   5.51954286e-01  -5.29081848e-01
    7.66924497e-01]
 [  1.10739121e-02   1.34962064e+00  -5.29081848e-01   5.07157222e-01
   -7.35143907e-01]
 [ -1.60520616e-02  -1.95632705e+00   7.66924497e-01  -7.35143907e-01
    1.06561938e+00]]

In [10]:
A = np.random.randn(4,3)
B = np.sum(A, axis = 1, keepdims = True)
print(B.shape)


(4, 1)

In [ ]:

Summary

forward

input: $a^{[l-1]}$

output: $a^{[l]}, cache: z^{[l]}, w^{[l]}, b^{[l]}$

vectorized:

$$Z^{[l]} = W^{[l]} · A^{[l-1]} + b^{[l}]$$$$A^{[l]} = g^{[l]}(Z^{[l]})$$

backward

input: $da^{[l]}$

output: $da^{[l-1]}, dW^{[l]}, db^{[l]}$

vectorized:

$$ dZ^{[l]} = dA^{[l]} * g^{[l] \prime}(Z^{[l]}) $$$$dW^{[l]} = \frac{1}{m} dZ^{[l]} · A^{[l-1]T}$$$$db^{[l]} = \frac{1}{m} np.sum(dZ^{[l]}, axis = 1, keepdims = True)$$$$dA^{[l-1]} = W^{[l]T} · dZ^{[l]}$$

In [ ]: